//
//  ZYTabBar.swift
//  ZYSinaWeibo
//
//  Created by 陟岳 on 16/5/20.
//  Copyright © 2016年 陟岳. All rights reserved.
//

import UIKit

class ZYTabBar: UITabBar {

    //初始化
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        addSubview(plusBtn)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置frame
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = self.bounds.width / 5
        let h = self.bounds.height
        var index: CGFloat = 0
        
        for subView in subviews {
        
            //判断是否时UITabBarButton类型下的控件（没有plusButton）
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                subView.frame = CGRect(x: w*index, y: 0, width: w, height: h)
                
                index += index == 1 ? 2 : 1
            }
        
        }
        
        plusBtn.frame = CGRect(x: w*2, y: 0, width: w, height: h)
        
    }
    
    //懒加载
    lazy var plusBtn: UIButton = {
    
        let btn = UIButton()
        
        //设置按钮的属性
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        
        return btn
    
    }()

}
